package com.lwm.annotationandreflection.ref;

import java.lang.annotation.*;

/**
 * @author liweimin
 * @date 2020/9/25 15:01
 * @description 反射操作注解 （ORM）
 */
public class ref11 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> clazz = Class.forName("com.lwm.annotationandreflection.ref.Woman");
        //通过反射获得注解
        Annotation[] annotations = clazz.getAnnotations();
//        for (Annotation annotation : annotations) {
//            System.out.println(annotation);
//        }
        //获得指定注解的值
        table annotation = clazz.getAnnotation(table.class);
        String value = annotation.value();
        System.out.println(value);
        //
        fileds name = clazz.getDeclaredField("name").getAnnotation(fileds.class);
        fileds age = clazz.getDeclaredField("age").getAnnotation(fileds.class);
        fileds id = clazz.getDeclaredField("id").getAnnotation(fileds.class);
    }
}

@table("db_woman")
class Woman {
    @fileds(columnName = "id", type = "int", length = 10)
    private int id;
    @fileds(columnName = "age", type = "int", length = 10)
    private int age;
    @fileds(columnName = "name", type = "String", length = 25)
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Woman(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Woman{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public Woman() {
    }
}

/**
 * 类名注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface table {
    String value();
}

/**
 * 属性注解
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface fileds {
    String columnName();

    String type();

    int length();
}